package com.aaa.backsystem.controller;


import com.aaa.backsystem.entity.LayuiTable;
import com.aaa.backsystem.entity.Result;
import com.aaa.backsystem.entity.WxTask;
import com.aaa.backsystem.service.WxTaskService;
import com.aaa.backsystem.util.MyConstants;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gyc
 * @since 2020-08-22
 */
@Controller
@RequestMapping("/wxTask")
@Api(tags = "巡检任务模块接口规范说明")
public class WxTaskController extends BaseController {

    @Autowired
    private WxTaskService wxTaskService;

    /**
     * 查询任务列表
     * @return
     */
    @ApiOperation(value = "查询任务列表",
            notes = "<span style='color:red;'>描述:</span>&nbsp;查询任务列表")
    @GetMapping("findTask/{page}/{limit}")
    @ResponseBody
    public LayuiTable findTask(@PathVariable("page") Integer page,@PathVariable("limit") Integer limit){
        LayuiTable table = new LayuiTable();

        Wrapper<WxTask> wrapper = new EntityWrapper();int count = wxTaskService.selectCount(wrapper);Page<WxTask> pageInfo = new Page(page, limit);
        List<WxTask> wxTaskList = wxTaskService.selectPage(pageInfo).getRecords();

        //从分页结果中提取list集合
        table.setCode(MyConstants.OPERATION_SUCCESS_CODE);
        table.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
        table.setData(wxTaskList);table.setCount(count);
        return table;
    }

    /**
     * 添加任务
     * @return
     */
    @ApiOperation(value = "添加任务",
            notes = "<span style='color:red;'>描述:</span>&nbsp;添加任务")
    @PostMapping("/addTask")
    @ResponseBody
    public Result addTask(@RequestBody WxTask wxTask){
        wxTask.setStatus("未巡查");
        try {
            wxTaskService.insert(wxTask);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return error();
    }

    /**
     * 删除任务
     * @return
     */
    @ApiOperation(value = "删除任务",
            notes = "<span style='color:red;'>描述:</span>&nbsp;删除任务")
    @DeleteMapping("/deleteTask/{id}")
    @ResponseBody
    public Result deleteTask(@PathVariable("id") Integer id){
        try {
            wxTaskService.deleteById(id);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return error();
    }

    /**
     * 修改任务状态
     * @return
     */
    @ApiOperation(value = "修改任务状态接口",
            notes = "<span style='color:red;'>描述:</span>&nbsp;巡检人员领取、结束任务时改变任务状态（未巡检，巡检中，巡检结束）")
    @PostMapping("/upadteTask")
    @ResponseBody
    public Result updateTask(@RequestBody WxTask wxTask){
        try {
            wxTaskService.updateById(wxTask);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return error();
    }
}

